package com.hhxy.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author ghp
 * @date 2022/9/8
 */
@WebServlet("/requestMethodServlet")
public class RequestMethodServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getMethod();
        PrintWriter pw = response.getWriter();
        String description = null;
        if("GET".equals(method)){
            //如果是使用GET方式请求
            description = "This is doGet method!";
        }
        if("POST".equals(method)){
            //如果是使用POST方式请求
            description = "This is doPost method";
        }
        //设置显示html文本，同时设置编码，防止中文乱码
        response.setContentType("text/html;charset=utf-8");
        //响应数据到网页
        description = "<h2>"+description+"</h2>";
        pw.write(description);
    }
}